(function() {
  var CSSTRANSFORM, Slideshow, TRANSITION_END_EVENTS, TRANSITION_PREFIXED, slideshow,
    bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };

  CSSTRANSFORM = Modernizr.prefixed("transform");

  TRANSITION_PREFIXED = Modernizr.prefixed('transition');

  TRANSITION_END_EVENTS = {
    'WebkitTransition': 'webkitTransitionEnd',
    'MozTransition': 'transitionend',
    'OTransition': 'oTransitionEnd',
    'msTransition': 'MSTransitionEnd',
    'transition': 'transitionend'
  };

  Slideshow = (function() {
    function Slideshow($el, timer) {
      this.resize = bind(this.resize, this);
      this.clearAutoplay = bind(this.clearAutoplay, this);
      this.setAutoplay = bind(this.setAutoplay, this);
      this.navigate = bind(this.navigate, this);
      this.revolve = bind(this.revolve, this);
      this.snapToGrid = bind(this.snapToGrid, this);
      this.updateActiveSlides = bind(this.updateActiveSlides, this);
      this.loop = bind(this.loop, this);
      this.$slideshow = $el;
      this.$slider = this.$slideshow.find('.js_slider');
      this.$slides = this.$slider.find('.js_slide');
      this.slides = $.makeArray(this.$slides);
      this.width = this.$slider.width();
      this.isScrolling = false;
      this.isAnimating = false;
      this.startPos = [];
      this.deltaX = null;
      this.nextElIndex = null;
      this.prevElIndex = null;
      this.timerDuration = timer || null;
      this.timer = this.setAutoplay(this.timerDuration);
      this.transforms = [];
      this.loop();
      this.$slideshow.on(TRANSITION_END_EVENTS[TRANSITION_PREFIXED], (function(_this) {
        return function(e) {
          _this.isAnimating = false;
          if (e.target === _this.slides[_this.prevElIndex]) {
            e.target.style.transition = 'none';
            e.target.style[CSSTRANSFORM] = '';
            setTimeout(function() {
              return e.target.style.transition = '';
            }, 20);
          }
          if (!_this.timer) {
            _this.setAutoplay();
          }
        };
      })(this));
      this.$slideshow.on('click', '.js_next, .js_prev', (function(_this) {
        return function(e) {
          var offset;
          if (_this.isAnimating) {
            return;
          }
          _this.clearAutoplay(_this.timer);
          offset = $(e.target).hasClass('js_prev') ? -1 : 1;
          _this.navigate(offset);
        };
      })(this));
      $(window).on('resize', (function(_this) {
        return function(e) {
          return _this.resize();
        };
      })(this));
      this.$slider.on('touchstart', (function(_this) {
        return function(e) {
          var touch;
          _this.deltaX = null;
          touch = e.originalEvent.targetTouches[0];
          _this.startPos = [touch.clientX, touch.clientY];
        };
      })(this));
      this.$slider.on('touchmove', (function(_this) {
        return function(e) {
          var activeSlides, deltaPos, touch;
          touch = e.originalEvent.targetTouches[0];
          deltaPos = [touch.clientX - _this.startPos[0], touch.clientY - _this.startPos[1]];
          _this.deltaX = deltaPos[0];
          activeSlides = _this.updateActiveSlides();
          if (Math.abs(_this.deltaX) < Math.abs(deltaPos[1])) {
            _this.isScrolling = true;
            return;
          }
          if (Math.abs(_this.deltaX) > _this.width * 1.25) {
            return;
          }
          if (!_this.isDragging) {
            _this.isDragging = true;
            _this.isScrolling = false;
            _this.clearAutoplay(_this.timer);
            activeSlides.prev.style.transition = 'none';
            activeSlides.prev.style[CSSTRANSFORM] = 'translate3d(-100%,0,0)';
          }
          if (activeSlides) {
            _this.transforms.push({
              el: activeSlides.prev,
              prop: CSSTRANSFORM,
              style: 'translate3d(' + (-_this.width + _this.deltaX) + 'px,0,0)'
            });
            _this.transforms.push({
              el: activeSlides.current,
              prop: 'transition',
              style: 'none'
            });
            _this.transforms.push({
              el: activeSlides.current,
              prop: CSSTRANSFORM,
              style: 'translate3d(' + _this.deltaX + 'px,0,0)'
            });
            _this.transforms.push({
              el: activeSlides.next,
              prop: 'transition',
              style: 'none'
            });
            _this.transforms.push({
              el: activeSlides.next,
              prop: CSSTRANSFORM,
              style: 'translate3d(' + (_this.width - _this.deltaX / -1) + 'px,0,0)'
            });
          }
        };
      })(this));
      this.$slider.on('touchend', (function(_this) {
        return function(e) {
          var offset, touch;
          _this.isDragging = false;
          if (_this.isScrolling) {
            return;
          }
          touch = e.originalEvent.changedTouches[0];
          offset = _this.startPos[0] < touch.clientX ? -1 : 1;
          _this.snapToGrid(offset);
        };
      })(this));
    }

    Slideshow.prototype.loop = function() {
      return window.requestAnimationFrame((function(_this) {
        return function() {
          if (_this.isDragging) {
            $.each(_this.transforms, function(index, transform) {
              return transform.el.style[transform.prop] = transform.style;
            });
            _this.transforms = [];
          }
          return _this.loop();
        };
      })(this));
    };

    Slideshow.prototype.updateActiveSlides = function(offset) {
      var activeSlides;
      offset = offset || 1;
      this.nextElIndex = (this.slides.length + offset) % this.slides.length;
      this.prevElIndex = (this.slides.length + (offset / -1)) % this.slides.length;
      return activeSlides = {
        prev: this.slides[this.prevElIndex],
        current: this.slides[0],
        next: this.slides[this.nextElIndex]
      };
    };

    Slideshow.prototype.snapToGrid = function(offset) {
      var activeSlides;
      this.isAnimating = true;
      activeSlides = this.updateActiveSlides(offset);
      activeSlides.prev.style.transition = 'none';
      activeSlides.prev.style[CSSTRANSFORM] = 'translate3d(100%,0,0)';
      activeSlides.current.classList.remove('active');
      activeSlides.current.style.transition = '';
      activeSlides.current.style[CSSTRANSFORM] = 'translate3d(' + 100 * (offset / -1) + '%,0,0)';
      activeSlides.next.style.transition = '';
      activeSlides.next.style[CSSTRANSFORM] = '';
      activeSlides.next.classList.add('active');
      this.slides = this.revolve(this.slides, offset);
    };

    Slideshow.prototype.revolve = function(elements, offset) {
      if (offset < 0) {
        elements.unshift(elements.pop());
      } else {
        elements.push(elements.shift());
      }
      return elements;
    };

    Slideshow.prototype.navigate = function(offset) {
      var activeSlides;
      this.isAnimating = true;
      activeSlides = this.updateActiveSlides(offset);
      activeSlides.next.style.transition = 'none';
      activeSlides.next.style[CSSTRANSFORM] = 'translate3d(' + 100 * offset + '%,0,0)';
      setTimeout((function(_this) {
        return function() {
          return _this.snapToGrid(offset);
        };
      })(this), 20);
    };

    Slideshow.prototype.setAutoplay = function(timing) {
      if (!this.timerDuration) {
        return;
      }
      return this.timer = window.setInterval((function(_this) {
        return function() {
          _this.navigate(1);
        };
      })(this), this.timerDuration);
    };

    Slideshow.prototype.clearAutoplay = function(timer) {
      if (timer) {
        window.clearInterval(timer);
        this.timer = void 0;
      }
    };

    Slideshow.prototype.resize = function() {
      return this.width = this.$slider.width();
    };

    return Slideshow;

  })();

  slideshow = new Slideshow($('.js_slideshow'), 5000);

}).call(this);
